{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['x', 'a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "#from exceptions import Empty\n",
    "class Deque(object):\n",
    "    DEFAULT_CAPACITY = 5\n",
    "    def __init__(self):\n",
    "        self._data = [None] * Deque.DEFAULT_CAPACITY\n",
    "        self._size = 0\n",
    "        self._front = 0\n",
    "        \n",
    "    def __len__(self):\n",
    "        return self._size\n",
    "    \n",
    "    def is_empty(self):\n",
    "        return self._size == 0\n",
    "    \n",
    "    def front(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Deque is empty')\n",
    "        return self._data[self._front]\n",
    "    \n",
    "    def last(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Deque is empty')\n",
    "        return self._data[(self._front + self._size - 1) % len(self._data)]\n",
    "    \n",
    "    def add_first(self,e):\n",
    "        if self._size == len(self._data):\n",
    "            self._resize(2 * len(self._data))\n",
    "        self._front = (self._front - 1) % len(self._data)\n",
    "        self._data[self._front] = e\n",
    "        self._size += 1\n",
    "    \n",
    "    def add_last(self,e):\n",
    "        if self._size == len(self._data):\n",
    "            self._resize(2 * len(self._data))\n",
    "        avail = (self._front +self._size)%len(self._data)\n",
    "        self._data[avail] = e\n",
    "        self._size += 1\n",
    "        \n",
    "    def delete_first(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Deque is empty')\n",
    "        result = self._data[self._front]\n",
    "        self._data[self._front] = None\n",
    "        self._front = (self._front +1)%len(self._data)\n",
    "        self._size -= 1\n",
    "        return result\n",
    "    \n",
    "    def delete_last(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Deque is empty')\n",
    "        back = (self._front +self._size -1) % len(self._data)\n",
    "        result = self._data[back]\n",
    "        self._data[back] = None\n",
    "        self._size -= 1\n",
    "        return result\n",
    "        \n",
    "        \n",
    "    def _resize(self,cap):\n",
    "        old = self._data\n",
    "        self._data = [None] * cap\n",
    "        walk = self._front\n",
    "        for k in range(self._size):\n",
    "            self._data[k] = old[walk]\n",
    "            walk= (1 + walk) % len(old)\n",
    "        self._front = 0\n",
    "      \n",
    "    def __repr__(self) :\n",
    "        data = []\n",
    "        walk = self._front\n",
    "        for k in range(self._size) :\n",
    "            data.append(self._data[walk])\n",
    "            walk = (1 + walk ) % len(self._data)\n",
    "        return f'{ data }'\n",
    "    \n",
    "if __name__ =='__main__':\n",
    "    d = Deque()\n",
    "    \n",
    "    d.add_first('a')\n",
    "    d.add_last('b')\n",
    "    d.add_last('c')\n",
    "    d.add_first('x')\n",
    "    print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ArrayQueue :\n",
    "    DEFAULT_CAPACITY = 10\n",
    "    def __init__ ( self ):\n",
    "        self . _data = [ None ] * ArrayQueue .\n",
    "        DEFAULT_CAPACITY\n",
    "        self . _size = 0\n",
    "        self . _front = 0\n",
    "    def __len__ ( self ):\n",
    "        return self . _size\n",
    "    def is_empty ( self ):\n",
    "        return self . _size == 0\n",
    "    def first ( self ):\n",
    "        if self . is_empty ():\n",
    "            raise Empty (’Queue is empty ’)\n",
    "        return self . _data [ self . _front ]\n",
    "    def dequeue ( self ):\n",
    "        if self . is_empty ():\n",
    "            raise Empty (’Queue is empty ’)\n",
    "        result = self . _data [ self . _front ]\n",
    "        self . _front = ( self . _front + 1) % len (\n",
    "        self . _data )\n",
    "        self . _size -= 1\n",
    "        return result\n",
    "    def dequeue ( self ):\n",
    "        if self . is_empty ():\n",
    "            raise Empty (’Queue is empty ’)\n",
    "        result = self . _data [ self . _front ]\n",
    "        self . _front = ( self . _front + 1) % len (\n",
    "        self . _data )\n",
    "        self . _size -= 1\n",
    "        return result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
